<template>
  <div>
    <a-modal
      :visible.sync="visible"
      v-bind="$attrs"
      v-on="$listeners"
      :maskClosable="false"
      :footer="null"
      width="516px"
      :title="treeModelName"
      wrapClassName="user-modal-container"
      @cancel="closeModal()"
    >
      <div class="curve-modal-body">
        <a-row :gutter="0">
          <a-form-model ref="form" :model="form" :rules="formRules">
            <a-col :span="24">
              <a-form-model-item label="序号" prop="nodeSort">
                <a-input
                  v-model="form.nodeSort"
                  placeholder="请输入序号"
                ></a-input>
              </a-form-model-item>
            </a-col>
            <a-col :span="24">
              <a-form-model-item label="节点名称" prop="nodeName">
                <a-input
                  v-model="form.nodeName"
                  placeholder="请输入节点名称"
                ></a-input>
              </a-form-model-item>
            </a-col>
          </a-form-model>
        </a-row>
      </div>
      <div class="modal-footer">
        <a-button type="default" @click="closeModal()">取消 </a-button>
        <a-button type="primary" :loading="loading" @click="save()">
          确定
        </a-button>
      </div>
    </a-modal>
  </div>
</template>

<script>
import { notificationTip } from "@/utils";
import { getLoginData } from "@/tools";
import { getBlockList } from "@/api/wisdom-beam/material-app";
import {
  getFileManageTreeSave,
  getFileManageTreeUpdate,
} from "@/api/wisdom-beam/standard-costs-app";
export default {
  props: {
    visible: {
      type: Boolean,
      default: false,
    },
    treeModelName: {
      type: String,
      default: "新增父节点",
    },
    treeForm: {
      type: Object,
      default: () => {
        return {};
      },
    },
  },
  watch: {
    visible: {
      handler(val) {
        this.$emit("update:visible", val);
        if (val) {
          this.getSecData();
          if (this.treeForm.treeId) {
            this.getDetail();
          }
        }
      },
      immediate: true,
    },
  },
  data() {
    return {
      form: {
        nodeSort: "",
        nodeName: "",
        zhgdDeptCode: undefined,
        parentNode: "",
      },
      formRules: {
        nodeSort: [
          {
            required: true,
            message: "请输入序号",
          },
        ],
        nodeName: [
          {
            required: true,
            message: "请输入节点名称",
          },
        ],
      },
      loading: false,
    };
  },
  mounted() {},
  methods: {
    //获取所有标段
    getSecData() {
      let { userId, tenantCode } = getLoginData();
      getBlockList({ tenantCode, userId })
        .then((res) => {
          let list = res?.data?.result || [];
          this.form.zhgdDeptCode = list?.[0]?.orgCode || [];
        })
        .catch((_) => {
          this.form.zhgdDeptCode = "";
        });
    },
    //详情
    getDetail() {
      this.form.nodeSort = this.treeForm.nodeSort;
      this.form.nodeName = this.treeForm.nodeName;
      this.form.zhgdDeptCode = this.treeForm.parentDeptCode;
      this.form.parentNode = this.treeForm.parentNode;
    },
    // 保存
    save() {
      this.$refs.form.validate(async (valid) => {
        if (valid) {
          this.loading = true;
          let { form } = this;
          let formJson = {
            nodeSort: form.nodeSort,
            nodeName: form.nodeName,
            zhgdDeptCode: form.zhgdDeptCode || this.treeForm.parentDeptCode,
            parentNode: this.treeForm.parentNode,
            type: "lcSandard",
          };
          if (
            this.treeModelName == "新增父节点" ||
            this.treeModelName == "新增子节点"
          ) {
            getFileManageTreeSave(formJson)
              .then((res) => {
                this.loading = false;
                this.closeModal();
                this.$parent.getTreeData();
                notificationTip("success", "新增成功!");
              })
              .catch((_) => {
                this.loading = false;
              });
          }
          if (
            this.treeModelName == "编辑父节点" ||
            this.treeModelName == "编辑子节点"
          ) {
            formJson.id = this.treeForm.treeId;
            getFileManageTreeUpdate(formJson)
              .then((res) => {
                this.loading = false;
                this.closeModal();
                this.$parent.getTreeData();
                notificationTip("success", "编辑成功!");
              })
              .catch((_) => {
                this.loading = false;
              });
          }
        }
      });
    },
    //关闭弹框
    closeModal() {
      this.$refs.form.resetFields();
      this.form = {
        nodeSort: "",
        nodeName: "",
        zhgdDeptCode: undefined,
        parentNode: "",
      };
      this.$emit("update:visible", false);
    },
  },
};
</script>

<style lang="less" scoped>
//弹框样式
/deep/.user-modal-container {
  .ant-modal-body {
    padding: 0;
  }
  .curve-modal-body {
    padding: 20px;
    max-height: 600px;
    .custom-scrollbar();
  }
  .modal-footer {
    padding-bottom: 20px;
    text-align: center;
    button {
      &:last-child {
        margin-left: 20px;
      }
    }
  }
  .ant-form-item {
    display: flex;
    .ant-form-item-label {
      width: 90px;
      white-space: pre-wrap;
      display: flex;
      justify-content: flex-end;
      line-height: 20px;
      align-items: center;
      margin-right: 15px;
      height: 36px;
      label {
        color: #999999;
      }
    }
    .check-item-select {
      height: 86px;
      .ant-select-selection__rendered {
        height: 86px;
        .ant-select-selection__placeholder {
          margin-top: -40px !important;
        }
      }
    }
    .ant-select {
      width: 100% !important;
      min-width: unset !important;
    }
    .ant-form-item-control-wrapper {
      flex: 1;
      overflow: hidden;
      white-space: nowrap;
      text-overflow: ellipsis;
    }
  }
}
</style>
